﻿CREATE FUNCTION [dbo].[fn_ColumnList]
(
	@Table sysname
)
RETURNS VARCHAR(MAX)
AS
BEGIN
	DECLARE @ColumnList  VARCHAR(MAX)
	
	SELECT @ColumnList = COALESCE(@ColumnList + ',', '') + c.[name]
	FROM   sys.tables t
	       INNER JOIN sys.[columns] c
	            ON  c.[object_id] = t.[object_id]
	WHERE  t.[name] = @Table
	       AND t.[type] = 'U'
	       AND c.[name] NOT IN (SELECT ColumnName
	                            FROM   dbo.fn_HousekeepingColumns())
	
	RETURN @ColumnList
END